﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace HRZoneTrainer
{
    [Serializable]
    public class Session
    {
        public Session()
        {
        }

        public Session(workout thisWorkout) {
            CurrentWorkout = thisWorkout;
            Initialise();
        }

        public void Initialise() {
            AverageHR = 0;
            AverageHRDataCount = 0;
            MaximumHR = 0;
            ResetHRData();
            CurrentTick = 0;
            Duration = 3600;
        }

        private void ResetHRData()
        {
            HRValues = new List<double>();
            HRPercentageValues = new List<double>();
            TimeValues = new List<double>();
        }

        internal void AddHRValue(int newHR, string percentageOf)
        {
            HRValues.Add(newHR);
            double HR = ( percentageOf == "Threshold" ) ? UserThresholdHR : UserMaximumHR;
            HRPercentageValues.Add(( newHR * 100.0 ) / HR);
            TimeValues.Add(CurrentTick);

            if ( newHR > MaximumHR )
            {
                MaximumHR = newHR;
            }
        }

        internal void AddAveHRValue(int newHR)
        {
            AverageHRDataCount++;
            AverageHR = AverageHR + ( ( newHR - AverageHR ) / AverageHRDataCount );  
        }

        [XmlIgnore]
        public double Duration
        {
            set { }
            get
            {
                return ( double.Parse(CurrentWorkout.duration) );
            }
        }

        [XmlIgnore]
        public int NumberOfPhases
        {
            get
            {
                return ( CurrentWorkout.events.Length );
            }
        }

        [XmlIgnore]
        public string CurrentPhaseTitle
        {
            get
            {
                return (CurrentWorkout.events[CurrentPhase].title);
            }
        }

        [XmlIgnore]
        public string CurrentPhaseDescription
        {
            get
            {
                return ( CurrentWorkout.events[CurrentPhase].description );
            }
        }

        [XmlIgnore]
        public int CurrentPhaseDuration
        {
            get
            {
                return ( Int32.Parse(CurrentWorkout.events[CurrentPhase].duration) );
            }
        }

        [XmlIgnore]
        public int CurrentPhaseIntensity
        {
            get
            {
                return ( Int32.Parse(CurrentWorkout.events[CurrentPhase].intensity) );
            }
        }

        [XmlIgnore]
        public int CurrentPhase { 
            get 
            {
                int cumulativePhase = 0;
                int foundPhase = 0;

                foreach ( workoutEventsPhase thisPhase in CurrentWorkout.events )
                {
                    cumulativePhase += Int32.Parse(thisPhase.duration);

                    if ( CurrentTick < cumulativePhase )
                    {
                        break;
                    }

                    foundPhase++;
                }

                return ( foundPhase );
            } 
        }

        [XmlIgnore]
        public int CurrentPhaseTimeRemaining
        {
            get
            {
                return ( CurrentPhaseDuration - ( CurrentTick - PhaseTimeSoFar(CurrentPhase) ) );
            }
        }

        [XmlIgnore]
        public bool CurrentPhaseCollectAverageHR
        {
            get {
                return ((CurrentWorkout.events[CurrentPhase].collectaveragehrdata == "true") ? true : false);
            }
        }

        private int PhaseTimeSoFar(int currentPhase)
        {
            int phaseTimeSoFar = 0;

            for ( int x = 0; x < currentPhase; x++ )
            {
                phaseTimeSoFar += Int32.Parse(CurrentWorkout.events[x].duration);
            }

            return ( phaseTimeSoFar );
        }

        [XmlIgnore]
        public int CurrentTick { get; set; }

        [XmlIgnore]
        public workout CurrentWorkout { get; set; }

        public List<double> HRPercentageValues { get; set; }
        public List<double> HRValues { get; set; }
        public List<double> TimeValues {get; set; }

        public double UserThresholdHR { get; set; }
        public double UserMaximumHR { get; set; }

        public double MaximumHR { get; set; }
        public double AverageHR { get; set; }
        public double AverageHRDataCount { get; set; }

        public string GetSaveFileName()
        {
            return (CurrentWorkout.name + "." +
               DateTime.Now.ToShortDateString().Replace('/', '-') + ".(" +
               DateTime.Now.ToLongTimeString().Replace(':', '.') + ")" + 
               ".xml");
        }
    }
}
